package com.maxuxu.scihome.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maxuxu.scihome.mapper.DocumentMapper;
import com.maxuxu.scihome.pojo.Document;
import com.maxuxu.scihome.pojo.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: DocumentService
 * @author: Maxuxu
 * @date: 2022/7/1 22:04
 * @description: 文档的增删改查
 */
@Service
public class DocumentService {
    @Autowired
    DocumentMapper documentMapper;
    @Autowired
    Document document;
    @Autowired
    Pages pages;

    /**
     * 插入文档
     * @param document
     */
    public void insertDocu(Document document){

        documentMapper.insert(document);

    }

    /**
     * 查询所有的文档，弃用，使用分页查询
     * @return
     */
    public List<Document> findAll(){

        List<Document> documents = documentMapper.selectList(null);
        return documents;
    }

    /**
     * 分页查询所有的文档信息
     * @param current
     * @param size
     * @return
     */
    public Pages selectPage(long current, long size){
        Page<Document> documentPage = documentMapper.selectPage(new Page<>(current, size), null);
        List<Document> records = documentPage.getRecords();
        long total = documentPage.getTotal();
        long pages = documentPage.getPages();
        long current1 = documentPage.getCurrent();
        long size1 = documentPage.getSize();
        this.pages.setPages(pages);
        this.pages.setCurrent(current1);
        this.pages.setSize(size1);
        this.pages.setTotal(total);
        this.pages.setRecords(records);
        return this.pages;
    }

    /**
     * 通过任意条件查询指定文档
     * @param document
     * @return
     */
    public Document selectByAny(Document document){
        QueryWrapper<Document> wrapper = new QueryWrapper<>();
        wrapper.eq("title", document.getTitle());
        Document selectOne = documentMapper.selectOne(wrapper);
        return selectOne;
    }

    /**
     * 通过id列表删除多个文档
     * @param list
     */
    public void deletDocument(List<Integer> list){
        documentMapper.deleteBatchIds(list);
    }

    /**
     * 根据任意条件更新文档内容,此处根据就标题改内容
     * @param document
     */
    public void updateDocument(String oldtitle,Document document){
        UpdateWrapper<Document> wrapper = new UpdateWrapper<>();
        wrapper.eq("title", oldtitle);
        documentMapper.update(document, wrapper);

    }

    /**
     * 分页查询的进阶操作（重写分页查询功能）
     * @param current
     * @param size
     * @return
     */
    public Pages selectpage2(long current, long size){
        QueryWrapper<Document> wrapper = new QueryWrapper<>();
        Page<Document> page = new Page<>(current, size, false);
        IPage<Document> documentPage = documentMapper.selectByPage(page, wrapper);
        List<Document> records = documentPage.getRecords();
        long total = documentPage.getTotal();
        long pages = documentPage.getPages();
        long current1 = documentPage.getCurrent();
        long size1 = documentPage.getSize();
        this.pages.setPages(pages);
        this.pages.setCurrent(current1);
        this.pages.setSize(size1);
        this.pages.setTotal(total);
        this.pages.setRecords(records);
        return this.pages;
    }
}
